package com.pengweiwei.androidzygote.functions.csutomviews;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.pengweiwei.androidzygote.R;
import com.putong35.common.baseactfrag.activity.BaseActivity;
import com.putong35.common.baseactfrag.view.CenterToolbar;
import com.putong35.common.utils.EasyToast;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;


public class CustomViewsIndexActivity extends BaseActivity {
    public static final String TAG = CustomViewsIndexActivity.class.getSimpleName();

    @BindView(R.id.toolbar1)
    CenterToolbar toolbar1;

    public static void start(Context fromContext) {
        Intent intent = new Intent(fromContext, CustomViewsIndexActivity.class);
        fromContext.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("自定义UI");

        actToolbar();
    }


    Disposable toolbarDisposable;

    private void actToolbar() {
        Observable.interval(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        toolbarDisposable = d;
                    }

                    @Override
                    public void onNext(Long value) {
                        Log.e(TAG, "onNext: value = "+value);
                        switch (value.intValue()) {
                            case 0:
                                toolbar1.setTitle("toolbar");
                                toolbar1.enableBackButton();
                                break;
                            case 1:
                                toolbar1.addLeftButton("left", null);
                                break;
                            case 2:
                                toolbar1.addLeftImageButton(R.mipmap.ic_launcher, v -> {
                                    EasyToast.showToast(CustomViewsIndexActivity.this, "left image button clicked");});
                                break;
                            case 3:
                                toolbar1.addRightButton("right", null);
                                break;
                            case 4:
                                toolbar1.addRightImageButton(R.mipmap.ic_launcher, v -> {
                                    EasyToast.showToast(CustomViewsIndexActivity.this, "right image button clicked");});
                                break;
                            case 5:
                                toolbar1.clearLeft();
                                toolbar1.clearRight();
                                break;

                            case 6: // 自定义toolbar中间部分
                                View centerView = getLayoutInflater().inflate(R.layout.custom_view_my_toolbar, new LinearLayout(CustomViewsIndexActivity.this), false);
                                toolbar1.setCenterView(centerView);
                                break;

                            case 7: // 自定义整条toolbar
                                /*
                                View toolbar = getLayoutInflater().inflate(R.layout.custom_view_my_toolbar, new FrameLayout(CustomViewsIndexActivity.this), false);
                                toolbar1.removeAllViews();
                                toolbar1.addView(toolbar);
                                */
                                toolbar1.removeAllViews();
                                getLayoutInflater().inflate(R.layout.custom_view_my_toolbar, toolbar1);
                                break;

                            default:
                                toolbarDisposable.dispose();

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: ");
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_custom_views_index, null);
    }

    @OnClick(R.id.btnViewPager)
    public void onBtnViewPagerClicked() {
        Intent intent = new Intent(this, ViewPagerActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btnRichTextBar)
    public void onBtnRichTextBarClicked() {
        Intent intent = new Intent(this, RichTextBarActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        toolbarDisposable.dispose();
    }
}